layui.use(['form'], function() {
    var form = layui.form
        , $ = layui.$;
    form.verify({
        classCode: (value) =>{
            if(!/^\d{9}/.test(value)){
                return '班级代码为9位数字';
            }
        },
        classNumber: (value) =>{
            if(value < 10 || value > 50){
                return '班级人数为10-50之间';
            }
        },
        name:(value) =>{
            if(value.length > 4){
                return '名称过长';
            }
        }
    });
    $('#classCode').blur(() =>{ //节点失焦事件
        var code = $('#classCode').val();
        if (!/^\d{9}/.test(value)) {
            layer.msg('班级代码为9位数字');
        }else{
            util.clashAjax($,'/Class/class/selCode', code);
        }
    })
    //通过session获取修改类型和键值
    var editType = sessionStorage.getItem('editType');
    var data = sessionStorage.getItem('data');
    if(editType == 1){
        let d = JSON.parse(data);
        //动态下拉框 学院
        util.getSelectAjax($,'/Class/college/getCollegeOption', 'college', d.college, null);
        //专业
        util.getSelectAjax($,'/Class/college/getMajorOption', 'major', d.major, d.college);
        //班导师
        util.getSelectAjax($,'/Class/user/getTutorOption', 'classTutor', d.classTutor, null);
        //表单数据设置
        form.val('class',{
            'id': d.id,
            'classCode': d.classCode,
            'classNumber': d.classNumber,
            'college': d.college,
            'major': d.major,
            'classTutor': d.classTutor,
            'classLink': d.classLink,
            'classLinkPhone': d.classLinkPhone
        })
        sessionStorage.setItem('editType',''); //清除session
        sessionStorage.setItem('data','');
    }else{
        util.getSelectAjax($,'/Class/college/getCollegeOption', 'college', null, null);
        util.getSelectAjax($,'/Class/user/getTutorOption', 'classTutor', null, null);
    }
    //联动下拉框
    form.on('select(college)', function(data){
        if(data != null){
            util.getSelectAjax($,'/Class/college/getMajorOption', 'major',null, data.value);
        }
    })
    //新增班级提交表单
    form.on('submit(submit)', function(data){
        if(editType == 1){
            util.crudAjax($,'/Class/class/update', data);
            window.parent.getAllCollege();
            window.parent.getAllMajor();
            window.parent.getAllTutor();
        }else{
            util.crudAjax($,'/Class/class/add', data);
            window.parent.getAllCollege();
            window.parent.getAllMajor();
            window.parent.getAllTutor();
        }
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
})